<?php
class DownloadsController extends AppController {

	var $name = 'Downloads';
	var $helpers = array('Html', 'Form');

	function index($id = null) {
		$this->Download->recursive = 0;
		if(isset($id)) {
			$this->set('downloads', $this->paginate(array('favorite_id'=>$id)));
		} else {
			$this->set('downloads', $this->paginate());
		}
		
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Download.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('download', $this->Download->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Download->create();
			if ($this->Download->save($this->data)) {
				$this->Session->setFlash(__('The Download has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Download could not be saved. Please, try again.', true));
			}
		}
		$favorites = $this->Download->Favorite->find('list');
		$this->set(compact('favorites'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Download', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Download->save($this->data)) {
				$this->Session->setFlash(__('The Download has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Download could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Download->read(null, $id);
		}
		$favorites = $this->Download->Favorite->find('list');
		$this->set(compact('favorites'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Download', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Download->del($id)) {
			$this->Session->setFlash(__('Download deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>